/**
 * MonitorWorkBenchServiceImpl.java
 * Created at 2017-3-22
 * Created by xieshuang
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.monitor.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.rms.monitor.dao.IMonitorWorkBenchDao;
import com.broadtext.rms.monitor.model.PackAlertView;
import com.broadtext.rms.monitor.model.PackLocationInfo;
import com.broadtext.rms.monitor.model.PlantPackTree;
import com.broadtext.rms.monitor.model.TtPackAlert;
import com.broadtext.rms.monitor.service.IMonitorWorkBenchService;

/**
 * <p>ClassName: MonitorWorkBenchServiceImpl</p>
 * <p>Description: 监控控制台服务实现类</p>
 * <p>Author: xieshuang</p>
 * <p>Date: 2017-3-22</p>
 */
@Service("monitorWorkBenchService")
public class MonitorWorkBenchServiceImpl extends EntityServiceImpl<TtPackAlert> implements IMonitorWorkBenchService {
    
    @Autowired
    @Qualifier("monitorWorkBenchDao")
    public void setEntityDao(EntityDao<TtPackAlert> entityDao) {
        super.entityDao = entityDao;
    }
    
    /**
     * 注入电池监控Dao
     */
    @Autowired
    @Qualifier("monitorWorkBenchDao")
    private IMonitorWorkBenchDao monitorWorkBenchDao;
    
    /**
     * Mapper请求前缀
     */
    private final String mapperModelName = "com.broadtext.rms.monitor.model.TtPackAlert";
    
    /**
     * 
     * <p>Description: 获取客户下电池告警信息</p>
     * @param pkId 主键id
     * @return 结果集
     * @throws DaoException 异常
     */
    @SuppressWarnings("unchecked")
	public Map<String, Object> findCustomPackAlertInfo(String pkId) throws DaoException {
        Map<String, Object> alertInfo;
        alertInfo = new HashMap<String, Object>();
        // 电池总量
        long total = 0;
        // 今日累计报警数量
        long todayAlertNum = 0;
        //充电中包含的掉电记录
        long packCharingNum_PowerFail = 0;
        //非充电中包含的掉电记录
        long packUnChargingNum_PowerFail = 0;
        
        total = this.monitorWorkBenchDao.findNumByCustomerId(this.mapperModelName+".findCustomerPackNum", pkId);
        todayAlertNum = this.monitorWorkBenchDao.findNumByCustomerId(this.mapperModelName+".findTotayAlertNum", pkId);
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        queryMap.put("pkId", pkId);
        //status : 0 正在充电
        queryMap.put("status", "0");
        packCharingNum_PowerFail = this.monitorWorkBenchDao.findChargingNumByCustomerId(this.mapperModelName+".findPowerFailNum", queryMap);
        //status : 1 其他状态
        queryMap.put("status", "1");
        packUnChargingNum_PowerFail = this.monitorWorkBenchDao.findChargingNumByCustomerId(this.mapperModelName+".findPowerFailNum", queryMap);
        
        alertInfo.put("total", total);
        alertInfo.put("todayAlertNum", todayAlertNum);
        alertInfo.put("charging", packCharingNum_PowerFail);
        alertInfo.put("unCharging", packUnChargingNum_PowerFail);
        alertInfo.put("powerFail", total-packCharingNum_PowerFail-packUnChargingNum_PowerFail);
        
        //查询表格数据
        Map<String, Object> params;
        params = new HashMap<String, Object>();
        params.put("cusId", pkId);
        List<Map<String, String>> barChartData = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackAlert.getBarChartData", params);
        alertInfo.put("barChartData", barChartData);
        List<Map<String, String>> pieChartData = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackAlert.getPieChartData", params);
        alertInfo.put("pieChartData", pieChartData);
        
        return alertInfo;
    }
    
    /**
     * 
     * <p>Description: 获取报警列表list</p>
     * @param map 查询条件
     * @return 结果集
     * @throws DaoException 异常
     */
    @SuppressWarnings("unchecked")
    public List<PackAlertView> findPackAlertInfoList(Map<String, Object> map) throws DaoException {
        return this.entityDao.findByQuery(this.mapperModelName+".findPackAlertInfoList", map);
    }
    
    /**
     * 
     * <p>Description: 获取客户下厂区电池树</p>
     * @param id 客户id
     * @return 结果集
     * @throws DaoException 异常
     */
    @SuppressWarnings("unchecked")
    public List<PlantPackTree> findPlantPackTreeByCustomer(String id) throws DaoException {
        Map<String, Object> map;
        map = new HashMap<>();
        map.put("cusId", id);
        //关联厂区的电池
        List<PlantPackTree> listWithFactory;
        listWithFactory = this.entityDao.findByQuery(this.mapperModelName+".findPlantPackTree", map);
        //未关联厂区的电池
        List<PlantPackTree> listWithOutFactory;
        listWithOutFactory = this.entityDao.findByQuery(this.mapperModelName+".findPlantPackTreeTwo", map);
        if (!listWithOutFactory.isEmpty()){
            //添加一条厂区父级
            /*PlantPackTree ppTree;
            ppTree = new PlantPackTree();
            ppTree.setId("nofactory");
            ppTree.setName("未关联厂区");
            ppTree.setPid(id);
            listWithOutFactory.add(ppTree);*/
            listWithOutFactory.addAll(listWithFactory);
            return listWithOutFactory;
        }
        return listWithFactory;
    }
    
    /**
     * 
     * <p>Description: 查询监控电池位置信息</p>
     * @param packCodesArray 电池id数组
     * @param queryFlag 1 表示默认查询   2表示单电池id模糊查询
     * @return 结果集
     * @throws DaoException 异常
     */
    public List<PackLocationInfo> findPackLocationInfo(Map<String, Object> queryMap) throws DaoException {
		if ("1".equals(queryMap.get("flag").toString())) {
			return this.monitorWorkBenchDao.findPackLocationInfo(this.mapperModelName+".findLocationInfo", queryMap);
		} else {
			return this.monitorWorkBenchDao.findPackLocationInfo(this.mapperModelName+".findLocationInfoByCode", queryMap);
		}
        
    }
    
    /**
     * 
     * <p>Description: 获取报警类型list</p>
     * @return 结果集
     * @throws DaoException 
     */
	@SuppressWarnings("unchecked")
	public List<Map<String, String>> getAlertNameList(String sqlId) throws DaoException {
		return this.entityDao.findByQuery(this.mapperModelName+sqlId, null);
	}
	
	/**
	 * 
	 * <p>Description: 获取概览页面所需数据</p>
	 * @return 结果集
	 * @throws DaoException 异常
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> getOverviewData() throws DaoException {
		Map<String, Object> dataMap;
		dataMap = new HashMap<String, Object>();
		
		//1、今日报警总数
		long todayAlertNum = 0;
		todayAlertNum = this.monitorWorkBenchDao.findNumByCustomerId(this.mapperModelName+".findTotayAlertNumNew", null);
		dataMap.put("todayAlertNum", todayAlertNum);
		
		//2、电池上电总量
		long powerOnNum = 0;
		powerOnNum = this.monitorWorkBenchDao.findNumByCustomerId(this.mapperModelName+".findPowerOnNum", null);
		dataMap.put("powerOnNum", powerOnNum);
		
		return dataMap;
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> getAlertClassifyBarChartData() throws DaoException {
		Map<String, Object> dataMap;
		dataMap = new HashMap<String, Object>();
		//3、告警分类bar_chart_data
		List<Map<String, String>> alertClassifyBarChartData = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackAlert.getAlertDataGroupByAlertCode", null);
		dataMap.put("alertClassifyBarChartData", alertClassifyBarChartData);
		return dataMap;
		
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> getPowerOnDistributeData() throws DaoException {
		Map<String, Object> dataMap;
		dataMap = new HashMap<String, Object>();
		//4、上电分布 
		List<Map<String, String>> powerOnDistributeData = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackAlert.getPowerOnDistributeData", null);
		dataMap.put("powerOnDistributeData", powerOnDistributeData);
		return dataMap;
		
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> getMaterialTypeData() throws DaoException {
		Map<String, Object> dataMap;
		dataMap = new HashMap<String, Object>();
		//5、正负极材料bar_charData
		List<Map<String, String>> materialTypeData = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackAlert.getMaterialTypeData", null);
		dataMap.put("materialTypeData", materialTypeData);
		return dataMap;
		
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> getPackModelData() throws DaoException {
		Map<String, Object> dataMap;
		dataMap = new HashMap<String, Object>();
		//6、电池型号分布
		List<Map<String, String>> packModelData = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackAlert.getAlertDataGroupByPackModel", null);
		dataMap.put("packModelData", packModelData);
		return dataMap;
		
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> getAppticationData() throws DaoException {
		Map<String, Object> dataMap;
		dataMap = new HashMap<String, Object>();
		//7、应用方向
		List<Map<String, String>> appticationData = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackAlert.getApplicationData", null);
		dataMap.put("appticationData", appticationData);
		//应用方向明细
		List<Map<String, String>> appticationDetailData = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackAlert.getAppticationDetailData", null);
		dataMap.put("appticationDetailData", appticationDetailData);
		return dataMap;
		
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> getAppticationDetailData() throws DaoException {
		Map<String, Object> dataMap;
		dataMap = new HashMap<String, Object>();
		//应用方向明细
		List<Map<String, String>> appticationDetailData = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackAlert.getAppticationDetailData", null);
		dataMap.put("appticationDetailData", appticationDetailData);
		return dataMap;
		
	}
}
